package javapop.framework.parser;


public class CharRange extends Char {
	private Character min;
	private Character max;
	
	public CharRange(Character min, Character max) {
		if(min==null) {
			throw new IllegalArgumentException("Character min must not be <null>");
		}
		if(max==null) {
			throw new IllegalArgumentException("Character max must not be <null>");
		}

		if(min.charValue()>max.charValue()) {
			throw new IllegalArgumentException("Minimum character must be less than maximum character");
		}
		this.min = min;
		this.max = max;
		
		StringBuilder buf = new StringBuilder();
		buf.append("character in [");
		buf.append("'");
		buf.append(min);
		buf.append("'..");
		buf.append("'");
		buf.append(max);
		buf.append("']");
		description = buf.toString();
	}

	@Override
	public boolean acceptChar(Character ch) {
		if(ch==null)
			return false;
		return ch.charValue()>=min.charValue() && ch.charValue()<=max.charValue();
	}
	
	@Override
	public int hashCode() {
		return description.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj==null) return false;
		if(obj==this) return true;
		if(obj.getClass()!=this.getClass())
			return false;
		CharRange cobj = (CharRange) obj;
		return cobj.min.equals(min) && cobj.max.equals(max);
	}


}
